package com.woniuxy.patrol.controller;

import com.woniuxy.patrol.pojo.PatrolRoute;
import com.woniuxy.patrol.service.PatrolRecordService;
import com.woniuxy.patrol.service.PatrolRouteService;
import com.woniuxy.common.utils.ResponseUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.util.List;

/**
 * 巡更路线控制器（对应《巡更demo.docx》3.3节巡更路线管理功能）
 */
@RestController
@RequestMapping("/patrol/route")
public class PatrolRouteController {

    @Resource
    private PatrolRouteService patrolRouteService;

    /**
     * 按社区查询巡更路线（对应文档中"巡更路线列表页面筛选功能"）
     */
    @GetMapping("/list")
    public ResponseUtil getByCommunity(@RequestParam Long communityId) {
        return patrolRouteService.getByCommunityId(communityId);
    }

    /**
     * 添加巡更路线（对应文档6.25.1添加巡更路线功能）
     */
    @PostMapping("/add")
    public ResponseUtil addRoute(@RequestBody RouteWithPoints routeWithPoints) {
        return patrolRouteService.addRoute(routeWithPoints.getRoute(), routeWithPoints.getPointIds());
    }

    /**
     * 编辑巡更路线（对应文档中路线列表的"编辑"操作）
     */
    @PostMapping("/update")
    public ResponseUtil updateRoute(@RequestBody RouteWithPoints routeWithPoints) {
        return patrolRouteService.updateRoute(routeWithPoints.getRoute(), routeWithPoints.getPointIds());
    }

    /**
     * 删除巡更路线（对应文档中路线列表的"删除"操作）
     */
    @GetMapping("/delete")
    public ResponseUtil deleteRoute(@RequestParam Long routeId) {
        return patrolRouteService.deleteRoute(routeId);
    }

    /**
     * 查看路线详情（包含点位，对应文档6.25.2巡更地图查看功能）
     */
    @GetMapping("/detail")
    public ResponseUtil getRouteDetail(@RequestParam Long routeId) {
        return patrolRouteService.getRouteDetail(routeId);
    }

    /**
     * 内部静态类：接收路线与关联点位ID列表
     */
    public static class RouteWithPoints {
        private PatrolRoute route;
        private List<Long> pointIds;

        // getter和setter
        public PatrolRoute getRoute() { return route; }
        public void setRoute(PatrolRoute route) { this.route = route; }
        public List<Long> getPointIds() { return pointIds; }
        public void setPointIds(List<Long> pointIds) { this.pointIds = pointIds; }
    }



    @GetMapping("/export")
    public void exportRecords(@RequestParam Long communityId, HttpServletResponse response) {
        try {
            // 设置响应头
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 处理文件名编码（避免中文乱码）
            String fileName = URLEncoder.encode("巡更记录_" + LocalDate.now() + ".xlsx", "UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);

            // 生成Excel并写入响应流
            ByteArrayOutputStream outputStream = patrolRouteService.exportRecords(communityId);
            response.getOutputStream().write(outputStream.toByteArray());
            response.getOutputStream().flush();
        } catch (Exception e) {
            // 错误处理
            e.printStackTrace();
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }
    }
}